import com.sun.javaws.security.AppContextUtil;

/**
 * Created by cc
 * Description:
 * User: CZH
 * Date: 2022-09-17
 * Time: 14:49
 */
class ListNodeD{
    public int data;
    public ListNodeD prev;
    public ListNodeD next;

    public ListNodeD(int data){
        this.data = data;
    }
}

public class MyRealLinkedList {
    public ListNodeD head;
    public ListNodeD last;

    public MyRealLinkedList() {
        //this.head = new ListNodeD(-1);傀儡节点
    }

    //头插法
    public void addFirst(int data) {
        //创建个新节点
        ListNodeD node = new ListNodeD(data);
        //链表中没有节点，第一次插入
        if (this.head == null) {
            this.head = node;
        }else {
            //先把新的接上
            node.next = this.head;
            //再从老的拉线接上
            this.head.prev = node;
            //更新头结点
            this.head = node;
        }
    }
    //尾插法
    public void addLast(int data) {
        ListNodeD node = new ListNodeD(data);
        if (this.head == null) {
            this.head = node;
            this.last = node;
        }else {
            this.last.next = node;
            node.prev = this.last;
            this.last = node;
        }
    }


    //让节点运动到链表index位置
    public ListNodeD findIndex(int index) {
        if (index < 0 || index > size()) {
            System.out.println("index位置不合法");
            return null;
        }
        ListNodeD cur = this.head;
        while (index > 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }


    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {
        if (index < 0 || index > size()) {
            System.out.println("index位置不合法");
            return;
        }
        //头插法
        if (index == 0) {
            addFirst(data);
            return;
        }
        //尾插法
        if (index == size()) {
            addLast(data);
            return;
        }
        //中间插数据
        //让cur运动到要插入的位置
        ListNodeD cur = findIndex(index);
        ListNodeD node = new ListNodeD(data);
        //先设置node的前驱和后继，再改变前后两个节点.
        node.prev = cur.prev;
        node.next = cur;
        node.prev.next = node;
        cur.prev = node;
    }


    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        //如果链表为空
        if (this.head == null) {
            return false;
        }
        //开始找
        ListNodeD cur = this.head;
        while (cur != null) {
            if (cur.data == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //得到单链表的长度
    public int size() {
        //判断链表是否正常
        if (this.head == null) {
            return 0;
        }
        //开始遍历
        int count = 0;
        ListNodeD cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //打印双向链表
    public void display() {
        //判断链表是否正常
        if (this.head == null) {
            System.out.println("链表为空");
            return;
        }
        ListNodeD cur = this.head;
        while (cur != null){
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {

    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {

    }

    /**
     * 1、打断点->调试
     * 2、jps
     * 3、jmap -histo:live xxxxx > d:\\log.txt
     */
    public void clear() {

    }
}
